import * as api from "../api/api";
import request from "../utils/request";
import { setUserInfo, pageSize } from "../utils/common";

export default {
  namespace: "user",
  state: {
    user: {},
    admin: {},
    admins: { totalCount: 0, items: [] },
    adminSearchs: { pageIndex: 1, pageSize, keywords: undefined },
    users: { totalCount: 0, items: [] },
    userSearchs: { pageIndex: 1, pageSize, keywords: undefined, vipLevel: "" },
    roles: { items: [] },
    userCommisions: { totalCount: 0, items: [] },
    userCommisionSearchs: { pageIndex: 1, pageSize, userId: 0 },
    userCashes: { totalCount: 0, items: [] },
    allUserCashes: { totalCount: 0, items: [] },
    allUserCashSearchs: {
      pageIndex: 1,
      pageSize,
      verifyStatus: "",
      keywords: ""
    },
    userCashSearchs: { pageIndex: 1, pageSize, userId: 0 },
    spread0Users: { totalCount: 0, items: [] },
    spread0UserSearchs: { pageIndex: 1, pageSize, userId: 0 },
    spread1Users: { totalCount: 0, items: [] },
    spread1UserSearchs: { pageIndex: 1, pageSize, userId: 0 },
    userSales: { totalCount: 0, items: [] },
    userSaleSearchs: { pageIndex: 1, pageSize, disposed: undefined },
    userRefits: { totalCount: 0, items: [] },
    userRefitSearchs: { pageIndex: 1, pageSize, disposed: undefined },
    userPromotions: { totalCount: 0, items: [] },
    userPromotionSearchs: { pageIndex: 1, pageSize, status: undefined },
    userWallet: { totalCount: 0, items: [] },
    userWalletSearchs: { pageIndex: 1, pageSize, userId: 0 },
    userRedPacket: { totalCount: 0, items: [] },
    userRedPacketSearchs: { pageIndex: 1, pageSize, userId: 0 },
    userDividends: { totalCount: 0, items: [] },
    userDividendsSearchs: { pageIndex: 1, pageSize, userId: 0 },
    allUserDividends: { totalCount: 0, items: [] },
    allUserDividendsSearchs: { pageIndex: 1, pageSize, keywords: "" },
    userPoint: { totalCount: 0, items: [] },
    userPointSearchs: { pageIndex: 1, pageSize, userId: 0 }
  },
  effects: {
    /**
     * 登录
     * @param param0
     * @param param1
     */
    *login({ payload }, { call, put }) {
      const { form, callback } = payload;
      const user: api.SingleUserMallUsersVOAdminVO = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageLoginAsync({
          form
        }),
        callback
      );
      setUserInfo(user);
      //yield put({ type: 'save', payload: { user } });
      callback && callback(true);
    },
    /**
     * 获取管理员列表
     * @param param0
     * @param param1
     */
    *getAdmins({ payload }, { call, put }) {
      const { keywords, pageIndex, pageSize } = payload;
      const formKeywords = keywords,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const admins: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallUsersVOUserVO = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetAdminsAsync(
          {
            formKeywords,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: { admins, adminSearchs: { keywords, pageIndex, pageSize } }
      });
    },
    /**
     * 获取用户列表
     * @param param0
     * @param param1
     */
    *getUsers({ payload }, { call, put }) {
      const { keywords, vipLevel, pageIndex, pageSize } = payload;
      const formKeywords = keywords,
        formVipLevel = vipLevel,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const users: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallUsersVOUserVO = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetUsersAsync(
          {
            formKeywords,
            formVipLevel,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          users,
          userSearchs: { keywords, vipLevel, pageIndex, pageSize }
        }
      });
    },
    /**
     * 用户钱包一键充值
     * @param param0
     * @param param1
     */
    *chargeUsersWallet({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageChangeUsersWalletAsync(
          {
            form
          }
        )
      );
      callback && callback(true);
    },
    /**
     * 获取用户详情
     * @param param0
     * @param param1
     */
    *getUser({ payload }, { call, put }) {
      const { id } = payload;
      const formId = id;
      const user = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetUserAsync({
          formId
        })
      );
      yield put({ type: "save", payload: { user } });
    },
    /**
     *
     * 保存管理员
     * @param param0
     * @param param1
     */
    *saveAdmin({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageSaveAdminAsync(
          { form }
        )
      );
      const { adminSearchs } = yield select(({ user }) => user);
      yield put({ type: "getAdmins", payload: { ...adminSearchs } });
      callback && callback(true);
    },
    /**
     * 禁用或启用用户
     * @param param0
     * @param param1
     */
    *setActive({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageSetActiveAsync(
          { form }
        )
      );
      const { userSearchs } = yield select(({ user }) => user);
      yield put({ type: "getUsers", payload: { ...userSearchs } });
      callback && callback(true);
    },

    /**
     * 修改用户等级
     * @param param0
     * @param param1
     */
    *modifyUserLevel({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageUserLevelChangeAsync(
          { form }
        )
      );
      const { userSearchs } = yield select(({ user }) => user);
      yield put({ type: "getUsers", payload: { ...userSearchs } });
      callback && callback(true);
    },
    /**
     * 增加新用户
     * @param param0
     * @param param1
     */
    *addNewUser({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageAddNewUserAsync(
          { form }
        )
      );
      const { userSearchs } = yield select(({ user }) => user);
      yield put({ type: "getUsers", payload: { ...userSearchs } });
      callback && callback(true);
    },
    /**
     * 删除用户
     * @param param0
     * @param param1
     */
    *remove({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageRemoveAsync({
          form
        })
      );
      const { userSearchs } = yield select(({ user }) => user);
      yield put({ type: "getUsers", payload: { ...userSearchs } });
      callback && callback(true);
    },
    /**
     * 修改密码
     * @param param0
     * @param param1
     */
    *updatePassword({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageUpdatePasswordAsync(
          { form }
        )
      );
      callback && callback(true);
    },
    /**
     * 重置密码
     * @param param0
     * @param param1
     */
    *resetPassword({ payload }, { call, put }) {
      const { form, callback } = payload;
      const password = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageResetPasswordAsync(
          { form }
        )
      );
      callback && callback(password);
    },
    /**
     * 获取角色列表
     * @param param0
     * @param param1
     */
    *getRoles({ payload }, { call, put }) {
      const roles: api.AbpApplicationServicesDtoListResultDtoSingleUserMallRolesVORoleVO = yield request(
        api.ManageroleManageApiFetchParamCreactor.manageroleManageGetRolesAsync(
          {}
        )
      );
      yield put({ type: "save", payload: { roles } });
    },
    /**
     * 获取用户佣金明细
     * @param param0
     * @param param1
     */
    *getUserCommisions({ payload }, { call, put }) {
      const { userId, pageIndex, pageSize } = payload;
      const formUserId = userId,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const userCommisions = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetUserCommisionsAsync(
          {
            formUserId,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          userCommisions,
          userCommisionSearchs: { userId, pageIndex, pageSize }
        }
      });
    },

    /**
     * 获取用户提现明细
     * @param param0
     * @param param1
     */
    *getUserCashes({ payload }, { call, put }) {
      const { userId, pageIndex, pageSize } = payload;
      const formUserId = userId,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const userCashes = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetUserCashesAsync(
          {
            formUserId,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          userCashes,
          userCashSearchs: { userId, pageIndex, pageSize }
        }
      });
    },
    /**
     * 获取所有用户提现明细
     * @param param0
     * @param param1
     */
    *getAllUserCashes({ payload }, { call, put }) {
      const { verifyStatus, keywords, pageIndex, pageSize } = payload;
      const formVerifyStatus = verifyStatus,
        formKeywords = keywords,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const allUserCashes = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetAllUserCashesAsync(
          {
            formVerifyStatus,
            formKeywords,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          allUserCashes,
          allUserCashSearchs: { verifyStatus, keywords, pageIndex, pageSize }
        }
      });
    },
    /**
     * 获取所有用户分红记录
     * @param param0
     * @param param1
     */
    *getAllUserDividends({ payload }, { call, put }) {
      const { keywords, pageIndex, pageSize } = payload;
      const formKeywords = keywords,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const allUserDividends = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetAllUserDividendsAsync(
          {
            formKeywords,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          allUserDividends,
          allUserDividendsSearchs: { keywords, pageIndex, pageSize }
        }
      });
    },
    /**
     * 获取用户分红详情
     * @param param0
     * @param param1
     */
    *getUserDividends({ payload }, { call, put }) {
      const { userId, pageIndex, pageSize } = payload;
      const formUserId = userId,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const userDividends = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetAllUserDividendAsync(
          {
            formUserId,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          userDividends,
          userDividendsSearchs: { userId, pageIndex, pageSize }
        }
      });
    },

    /**
     * 审核用户提现明细
     * @param param0
     * @param param1
     */
    *verifyUserCashes({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { allUserCashSearchs } = yield select(({ user }) => user);
      let result = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageCashAuditAsync(
          { form }
        )
      );
      yield put({
        type: "getAllUserCashes",
        payload: { ...allUserCashSearchs }
      });
      callback && callback(result);
    },
    /**
     * 重新提现
     * @param param0
     * @param param1
     */
    *reCash({ payload }, { call, put }) {
      const { form, callback } = payload;
      const password = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageReCashAsync({
          form
        })
      );
      callback && callback(password);
    },
    /**
     * 获取用户一级推广
     * @param param0
     * @param param1
     */
    *getSpread0Users({ payload }, { call, put }) {
      const { userId, pageIndex, pageSize } = payload;
      const formUserId = userId,
        formLevel = 0,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const spread0Users = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetSpreadUsersAsync(
          {
            formUserId,
            formLevel,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          spread0Users,
          spread0UserSearchs: { userId, pageIndex, pageSize }
        }
      });
    },
    /**
     * 获取用户二级推广
     * @param param0
     * @param param1
     */
    *getSpread1Users({ payload }, { call, put }) {
      const { userId, pageIndex, pageSize } = payload;
      const formUserId = userId,
        formLevel = 1,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const spread1Users = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetSpreadUsersAsync(
          {
            formUserId,
            formLevel,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          spread1Users,
          spread1UserSearchs: { userId, pageIndex, pageSize }
        }
      });
    },

    /**
     * 卖车列表
     * @param param0
     * @param param1
     */
    *getUserSales({ payload }, { call, put }) {
      const { disposed, pageIndex, pageSize } = payload;
      const formDisposed = disposed,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const userSales = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetUserSalesAsync(
          {
            formDisposed,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          userSales,
          userSaleSearchs: { disposed, pageIndex, pageSize }
        }
      });
    },
    /**
     * 卖车处理
     * @param param0
     * @param param1
     */
    *userSaleDispose({ payload }, { call, put }) {
      const { form, callback } = payload;
      const password = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageUserSaleDisposeAsync(
          { form }
        )
      );
      callback && callback(password);
    },
    /**
     * 改装车列表
     * @param param0
     * @param param1
     */
    *getUserRefits({ payload }, { call, put }) {
      const { disposed, pageIndex, pageSize } = payload;
      const formDisposed = disposed,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const userRefits = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetUserRefitsAsync(
          {
            formDisposed,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          userRefits,
          userRefitSearchs: { disposed, pageIndex, pageSize }
        }
      });
    },
    /**
     * 改装车处理
     * @param param0
     * @param param1
     */
    *userRefitDispose({ payload }, { call, put }) {
      const { form, callback } = payload;
      const password = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageUserRefitDisposeAsync(
          { form }
        )
      );
      callback && callback(password);
    },
    /**
     * 推广大使申请列表
     * @param param0
     * @param param1
     */
    *getUserPromotions({ payload }, { call, put }) {
      const { status, pageIndex, pageSize } = payload;
      const formStatus = status,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const userPromotions = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetUserPromotionsAsync(
          {
            formStatus,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          userPromotions,
          userPromotionSearchs: { status, pageIndex, pageSize }
        }
      });
    },
    /**
     * 推广大使审核
     * @param param0
     * @param param1
     */
    *promotionAudit({ payload }, { call, put }) {
      const { form, callback } = payload;
      const password = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManagePromotionAuditAsync(
          { form }
        )
      );
      callback && callback(password);
    },
    /**
     * 获取用户体验金明细
     * @param param0
     * @param param1
     */
    *getUserPoint({ payload }, { call, put }) {
      const { userId, pageIndex, pageSize } = payload;
      const formUserId = userId,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const userPoint = yield request(
        api.ManageuserManageApiFetchParamCreactor.manageuserManageGetUserPointsAsync(
          {
            formUserId,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          userPoint,
          userPointSearchs: { userId, pageIndex, pageSize }
        }
      });
    }
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
    clear(state, action) {
      setUserInfo();
      return { ...state, user: {} };
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, search }) => {
        switch (pathname) {
          case "/user/admins":
            dispatch({ type: "getRoles" });
            dispatch({
              type: "getAdmins",
              payload: { pageIndex: 1, pageSize }
            });
            break;
        }
      });
    }
  }
};
